package com.suxin.watch;

import java.nio.file.*;
import java.util.concurrent.TimeUnit;

/**
 * @author Tang
 * @classname FileWatchService
 * @description [  ]
 * @date 2022/5/29 22:31
 */
public class FileWatchService {

    public static void main(String[] args) throws Exception {
        FileSystem fileSystem = FileSystems.getDefault();
        WatchService watchService = fileSystem.newWatchService();
        // 为path注册监听器
        Paths.get("G:\\suxin\\siks").register(watchService,
                StandardWatchEventKinds.ENTRY_CREATE,
                StandardWatchEventKinds.ENTRY_DELETE,
                StandardWatchEventKinds.ENTRY_MODIFY,
                StandardWatchEventKinds.OVERFLOW);
        while (true) {
            WatchKey watchKey = watchService.poll(1000, TimeUnit.MILLISECONDS);
            if (watchKey == null) {
                continue;
            }
            for (WatchEvent<?> pollEvent : watchKey.pollEvents()) {
                WatchEvent.Kind<?> kind = pollEvent.kind();
                System.out.println("poll event: " + kind + " name:" + kind.name());
            }
        }

    }

}